﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;

namespace MMITCore.CurrencyConverter
{
    public static class ExchangeRateCalculator
    {
        public static decimal Convert(decimal amount, string fromCurrency, string toCurrency)
        {
            WebClient web = new WebClient();
 
            string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
            //string url = string.Format("https://www.igolder.com/exchangerate.ashx?from={0}&to={1}", fromCurrency, toCurrency);
 
            string response = web.DownloadString(url);
 
            
 
            //Regex regex = new Regex("rhs: \\"(d*.d*)");
            Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
            
            Match match = regex.Match(response);
            string raw = match.Groups[1].Value.Replace(" ", "");
            decimal rate = System.Convert.ToDecimal(raw);
            //decimal rate = System.Convert.ToDecimal(response) * amount;
 
            return rate;
        }
    }
}